from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save 
import datetime
# Create your models here.

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique = True)
    experience = models.IntegerField(default=100)
    level = models.IntegerField(default=1)
    money= models.IntegerField(default=100)
    energy=models.IntegerField(default=100)
    total_energy=models.IntegerField(default=100)
    last_seen=models.DateTimeField(default=datetime.datetime.now())
    
    def __unicode__(self):
        return 'User[%s] \n Exp: %s \n Lvl: %s \n $:%s \n Energy: %s' % (self.user, self.experience, self.level, self.money, self.energy)
    

    def create_profile(sender, **kwargs):
            if 'created' in kwargs and 'instance' in kwargs:
                    if kwargs['created']:
                            profile = UserProfile(user=kwargs['instance'])
                            profile.save()

    post_save.connect(create_profile, sender=User)

class Item(models.Model):
    #user=models.ForeignKey(UserProfile)# A UserProfile has many Items
    name=models.CharField(max_length=25)
    description=models.CharField(max_length=300)
    level_required = models.IntegerField()
    cost=models.IntegerField()
    energy_boost=models.IntegerField()

    def __unicode__(self):
        return 'Item[%s, lvl rqrd: %s,$ %s]' % (self.name, self.level_required, self.cost)

class User_Item(models.Model):    
    theuser = models.ForeignKey(UserProfile)
    theitem = models.ForeignKey(Item)


class Activity(models.Model):
    name=models.CharField(max_length= 25)
    level = models.IntegerField()
    experience_reward=models.IntegerField()
    money_reward=models.IntegerField()
    description=models.CharField(max_length=300)
    answer=models.CharField(max_length=50)
    energy_consumed=models.IntegerField()
    
    def __unicode__(self):
        return 'Activity- %s [%s] \n Question: %s \n Answer: %s \n Reward($ %s, exp: %s). \n Energy consumed:%s ' % (self.name, self.level, self.description, self.answer, self.money_reward, self.experience_reward, self.energy_consumed)

class CompletedActivity(models.Model):
    theuser = models.ForeignKey(UserProfile)
    theactivity = models.ForeignKey(Activity)
